package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.viewVo.AttrResponseVo;
import com.atguigu.gulimall.product.viewVo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Message;



/**
 * 商品属性
 *
 * @author xiaolei
 * @email qingyulei@yeah.net
 * @date 2020-08-22 23:46:20
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;


    ///product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public Message updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities){

        productAttrValueService.updateSpuAttr(spuId,entities);

        return Message.ok();
    }

    @GetMapping("/base/listforspu/{spuId}")
    public Message baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);
        return Message.ok().put("data",entities);
    }

    @GetMapping("/{attrType}/list/{catelogId}")
    public Message getBaseAttrList(@RequestParam Map<String, Object> params,
                                   @PathVariable("catelogId") Long catelogId,
                                   @PathVariable("attrType") String attrType) {

        PageUtils pageUtils = attrService.queryBaseAttrPage(params, catelogId, attrType);
        return Message.ok().put("page", pageUtils);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public Message list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return Message.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public Message info(@PathVariable("attrId") Long attrId){
		AttrResponseVo attr = attrService.getAttrInfo(attrId);

        return Message.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public Message save(@RequestBody AttrVo attr){

		attrService.saveAttr(attr);
        return Message.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public Message update(@RequestBody AttrResponseVo attr){
		attrService.updateAttr(attr);

        return Message.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public Message delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return Message.ok();
    }

}
